package com.ecar.api.async;

import java.util.List;
import java.util.concurrent.BlockingQueue;

import com.ecar.api.service.common.NotifyService;

public class SMSExecutor implements Runnable{
	
	private BlockingQueue<List<String>> incomingQueue;

	private NotifyService notifyService;

	@Override
	public void run() {
		while (!Thread.currentThread().isInterrupted()) {
			try {
				List<String> list = incomingQueue.take();
				if (list != null && list.size() >= 2) {
					if (!notifyService.sendSMS(list.get(0), list.get(1))) {
						Thread.sleep(100);
						notifyService.sendSMS(list.get(0), list.get(1));
					}
				}
			} catch (InterruptedException ie) {
				Thread.currentThread().interrupt();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public SMSExecutor(BlockingQueue<List<String>> incomingQueue, NotifyService notifyService) {
		this.incomingQueue = incomingQueue;
		this.notifyService = notifyService;
	}
}
